package com.xgk.boot.module.core.controller.admin.payment;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgk.boot.framework.common.constant.ErrorCodeConstants;
import com.xgk.boot.framework.common.exception.util.ServiceExceptionUtil;
import com.xgk.boot.framework.common.pojo.CommonResult;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.security.core.util.SecurityFrameworkUtils;
import com.xgk.boot.module.core.controller.admin.payment.vo.payment.PaymentApproveVO;
import com.xgk.boot.module.core.controller.admin.payment.vo.payment.PaymentPageReqVO;
import com.xgk.boot.module.core.controller.admin.payment.vo.payment.PaymentPageRespVO;
import com.xgk.boot.module.core.controller.admin.payment.vo.payment.PaymentSaveReqVO;
import com.xgk.boot.module.core.dal.entity.customer.CustomerDO;
import com.xgk.boot.module.core.dal.iservice.PaymentRecordsService;
import com.xgk.boot.module.core.enums.biz.ApprovalTypeEnum;
import com.xgk.boot.module.core.service.customer.CustomerServiceImpl;
import com.xgk.boot.module.core.service.permission.UserRoleServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xgk.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 供应商")
@RestController
@RequestMapping("/admin/payment")
@Validated
public class PaymentController {

    @Resource
    private PaymentRecordsService paymentRecordsService;
    
    @Resource
    private UserRoleServiceImpl userRoleService;
    
    @Resource
    private CustomerServiceImpl customerService;

    @PostMapping("/create")
    @Operation(summary = "创建充值")
    public CommonResult<Long> createPayment(@Valid @RequestBody PaymentSaveReqVO createReqVO) {
        Boolean currentUserAdmin = userRoleService.isCurrentUserAdmin();
        if (!currentUserAdmin) {
            CustomerDO customerDO = customerService.selectByCustomerCode(SecurityFrameworkUtils.getLoginUser().getUsername());
            createReqVO.setCustomerId(customerDO.getId().toString());

        }
        return success(paymentRecordsService.createPayment(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "修改充值")
    public CommonResult<Boolean> updatePayment(@Valid @RequestBody PaymentSaveReqVO updateReqVO) {
        paymentRecordsService.updatePayment(updateReqVO);
        return success(true);
    }
    @PostMapping("/approve")
    @Operation(summary = "审核充值信息")
    @PreAuthorize("@ss.hasPermission('system:admin')")
    public CommonResult<Long> approvePayment(@Valid @RequestBody PaymentApproveVO approveVO) {
        Boolean currentUserAdmin = userRoleService.isCurrentUserAdmin();
        if (!currentUserAdmin) {
           throw new RuntimeException("非管理员不能审核充值信息");
        }
        return success(paymentRecordsService.approvePayment(approveVO));
    }

    @PostMapping("/discard")
    @Operation(summary = "废弃充值信息")
    public CommonResult<Long> discardPayment(@Valid @RequestBody PaymentApproveVO approveVO) {
        if(approveVO.getStatus().equals(ApprovalTypeEnum.CANCEL.getValue())) {
            return success(paymentRecordsService.approvePayment(approveVO));
        }
        throw ServiceExceptionUtil.exception(ErrorCodeConstants.ACTION_EXCEPTION,"Not Supported");
    }


    @GetMapping("/page")
    @Operation(summary = "获得充值信息")
    public CommonResult<PageResult<PaymentPageRespVO>> getPaymentPage(PaymentPageReqVO pageReqVO) {
        Boolean currentUserAdmin = userRoleService.isCurrentUserAdmin();
        if (!currentUserAdmin) {
            pageReqVO.setCustomerCode(SecurityFrameworkUtils.getLoginUser().getUsername());
        }
        Page<PaymentPageRespVO> paymentPage = paymentRecordsService.getPaymentPage(pageReqVO);
        PageResult<PaymentPageRespVO> paymentPageResult = new PageResult<>(paymentPage.getRecords(), paymentPage.getTotal());
        return success(paymentPageResult);
    }

}
